package com.web;

import com.bean.Book;
import com.dao.BookDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/book/list")
public class BookListServlet extends HttpServlet {
    /**
     * 这里没有具体的请求方法，所以用 service
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 创建对象
        BookDao bookDao = new BookDao();

        try {
            // 实现方法
            List<Book> books = bookDao.list();

            // 把信息存到 xxx 中处理
            req.setAttribute("xxx", books);
            // 页面转发
            req.getRequestDispatcher("/book/bookList.jsp").forward(req, resp);
        } catch (Exception e) {
            // 把异常信息打印到控制台
            e.printStackTrace();
            // 把信息存到 message 中处理
            req.setAttribute("message", "查询所有记录失败！");
            // 页面转发
            req.getRequestDispatcher("/book/bookResult.jsp").forward(req, resp);
        }
    }
}
